<template>
  <div class="home-container">
    <!-- 顶部栏 -->
    <van-nav-bar fixed placeholder>
      <template #left>
        <img src="../../assets/logo.png" alt="" width="100" />
      </template>
      <template #right>
        <van-button icon="search" round type="primary" @click="$router.push('/search')">搜索</van-button>
      </template>
    </van-nav-bar>
    <!-- 导航栏 -->
    <van-tabs v-model="active" @click="onRefresh()">
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <van-cell
              v-for="(item, index) in list"
              :key="index"
              :title="item.title"
              @click="$router.push({path:'/article',query:{id:item.art_id}})"
            >
              <template #label>
                <div>
                  <van-grid :column-num="item.cover.type">
                    <van-grid-item
                      v-for="(value, idx) in item.cover.images"
                      :key="idx"
                    >
                      <van-image :src="value" lazy-load/>
                    </van-grid-item>
                  </van-grid>
                </div>
                <div>
                  <span>{{ item.aut_name }}</span
                  >&nbsp; <span>{{ item.comm_count }}评论</span>&nbsp;
                  <span>{{ item.pubdate | fromNow }}</span>
                </div>
              </template>
            </van-cell>
          </van-list>
        </van-pull-refresh>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
// 频道列表
import { getChannelList } from './home.js'
// 文章列表
import { getArticleList } from './home.js'
// 时间插件
import moment from 'moment'

moment.locale('zh-cn');

export default {
  name: 'home',
  data () {
    return {
      active: 0,
      loading: false, // 上拉加载
      finished: false, // 加载完全部
      list: [],
      isLoading: false, // 下拉加载
      channels: [],
      timestamp: null
    }
  },
  methods: {
    // 上拉加载数据事件
    async onLoad () {
      if (!this.timestamp) {
        this.timestamp = Date.now()
      }
      let res = await getArticleList({
        channel_id: this.channels[this.active].id,
        timestamp: this.timestamp
      })
      this.list.push(...res.data.data.results)
      this.timestamp = res.data.data.pre_timestamp
      this.loading = false
      if (this.list.length >= 30) {
        this.finished = true
      }
    },
    // 下拉加载数据点击事件
    onRefresh () {
      this.timestamp = null
      this.finished = false
      this.list = []
      this.isLoading = false
      this.loading = true
      this.onLoad()
    }
  },
  async created () {
    let res = await getChannelList()
    this.channels = res.data.data.channels
  },
  filters: {
    fromNow(value) {
      return moment(value).fromNow(); 
    }
  }
}
</script>

<style lang="less">
.home-container {
  .van-nav-bar {
    z-index: 10;
  }
  .van-nav-bar__content {
    background: #0098fa;
    .van-button {
      width: 100px;
      height: 30px;
      background-color: #1dadfb;
      border: 0;
    }
    .van-icon {
      color: #fff;
    }
  }
  .van-tabs__wrap {
    position: fixed;
    top: 46px;
    z-index: 10;
    width: 100%;
  }
  .van-tabs__content {
    margin-top: 44px;
  }
}
</style>
